package com.qianheng.kit.base.java.thread.threads;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Supplier;

@Slf4j
public class Example {
    public static Supplier<String> supplierDemoTemplate(String name, int min, int max) {
        final String timeName = String.format("%s%s到%sms", name, min, max);
        return () -> {
            log.info("开始" + timeName);
            long start = System.currentTimeMillis();
            try {
                Thread.sleep(RandomUtil.randomInt(min, max));
            } catch (InterruptedException e) {
                log.error("异常" + timeName, e);
                throw new RuntimeException(e);
            }
            long l = System.currentTimeMillis() - start;
            log.info("结束" + timeName + " 耗时=" + l);
            return timeName + "#" + l;
        };
    }

    public static Supplier<String> 自动洗锅300到500ms =
            supplierDemoTemplate("自动洗锅", 300, 500);
    public static Supplier<String> 自动洗锅100到200ms =
            supplierDemoTemplate("自动洗锅", 100, 200);
    public static Supplier<String> 切菜配菜300到400ms =
            supplierDemoTemplate("切菜配菜", 300, 400);
    public static Supplier<String> 炒菜500到600ms =
            supplierDemoTemplate("炒菜", 500, 600);

    public static Supplier<String> 异常 =() -> {
        try {
            log.info("模拟异常开始");
            Thread.sleep(600);
            log.info("模拟异常 600ms后");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        throw new RuntimeException("模拟异常");
    };

}
